//
//  mySelfVC.swift
//  甜言蜜语
//
//  Created by yingli on 16/7/23.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit

class mySelfVC: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var headerView: UIView!
    @IBOutlet weak var nameLbl: UILabel!//用户名
    @IBOutlet weak var userImg: UIImageView!//头像
    @IBOutlet weak var integralNumLbl: UILabel!//积分数
    @IBOutlet weak var diamondsNumLbl: UILabel!//钻石
    @IBOutlet weak var collectionNumLbl: UILabel!//收藏
    @IBOutlet weak var concernNumLbl: UILabel!//关注
    @IBOutlet weak var fansNumLbl: UILabel!//粉丝
    @IBOutlet weak var topicNumLbl: UILabel!//话题
    @IBOutlet weak var activityNumLbl: UILabel!//活动
    @IBOutlet weak var advertisesNumLbl: UILabel!//招聘
    @IBOutlet weak var transferNumLbl: UILabel!//转让
    @IBOutlet weak var joinBtn: UIButton!//根据用户身份来定分为求职和招聘
    @IBOutlet weak var ivBtn: UIButton!
    weak var contentLineImageView: UIImageView!//nav下面的线条
    var modelList: mySelfInfo?{
        didSet {
            self.showNumber()
        }
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
       self.contentLineImageView = self.findHairlineImageViewUnder((self.navigationController?.navigationBar)!)
        
        let cellNib = UINib(nibName: "mySelfCell", bundle: nil)
        self.tableView.registerNib(cellNib, forCellReuseIdentifier: "mySelfCell")
        publicMethod.hiddleExtendCellForTableView(self.tableView)
        DataTool.getNumberInfo({ (newModel) in
            guard let newDataes = newModel else {
                return
            }
            
            print(newDataes)
            self.modelList = newDataes
        })

    }
   
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(true)
       
        self.navigationController?.navigationBar.barTintColor = UIColor.colorWithRGB(207, g: 209, b: 209)
        //调登录接口
        if ApplicationDelegate.isLogin(){
            let phone = publicMethod.userName()
            let pass = publicMethod.passWord()
            let token = publicMethod.token()
            DataTool.login(phone, pass: pass, token: token, completionHandler: { (newModel) in
                guard let newDataes = newModel else {
                    return
                }
                print(newDataes)
                
            })
        }

       self.contentLineImageView.hidden = true
        self.tabBarController?.tabBar.hidden = false
        
        DataTool.getNumberInfo({ (newModel) in
            guard let newDataes = newModel else {
                return
            }
            
            print(newDataes)
            self.modelList = newDataes
        })

    }
   

    override func viewWillDisappear(animated: Bool) {
        super.viewDidAppear(true)
        
        self.contentLineImageView.hidden = false
    }
    
    /**
     *  @brief 为了隐藏nav上面的黑线
     *
     *  @param view
     *
     *  @return
     */
    func findHairlineImageViewUnder(view: UIView) -> UIImageView? {
        
        if view is UIImageView && view.bounds.size.height <= 1.0 {
            return (view as! UIImageView)
        }
        let subviews = (view.subviews as [UIView])
        
        for subview: UIView in subviews {
            
        if let imageView: UIImageView = findHairlineImageViewUnder(subview) {
                return imageView
            }
        }
        
        return nil
    }
    
    func showNumber() -> Void {
       
        if !publicMethod.isComplete(){
            self.joinBtn.setTitle("招聘", forState: UIControlState.Normal)
        } else {
            let type = (NSUserDefaults.standardUserDefaults().objectForKey("k_UD_type")) as! String
            if type == "个人"{
                self.joinBtn.setTitle("求职", forState: UIControlState.Normal)
                self.advertisesNumLbl.text = modelList?.job
            }else{
                self.joinBtn.setTitle("招聘", forState: UIControlState.Normal)
                self.advertisesNumLbl.text = modelList?.recruitment//招聘
            }
            
        }
        
       if self.modelList?.name == ""{
        self.nameLbl.text = "请点击头像登录"
       }else{
        self.nameLbl.text = self.modelList?.name
       }
       
        self.userImg.sd_setImageWithURL(NSURL(string:(modelList?.timg)!), placeholderImage: (UIImage(named: "headImg")))//头像
        self.userImg.layer.cornerRadius = 40
        self.userImg.layer.masksToBounds = true
        self.integralNumLbl.text = modelList?.integral//积分数
        self.diamondsNumLbl.text = modelList?.diamond//钻石
         self.collectionNumLbl.text = modelList?.shoucangnum//收藏
        self.concernNumLbl.text = modelList?.guanzhunum//关注
         self.fansNumLbl.text = modelList?.fensinum//粉丝
         self.topicNumLbl.text = modelList?.huatinum//话题
         self.activityNumLbl.text = modelList?.activitynum//活动
        
         self.transferNumLbl.text = modelList?.transfer//转让
    }
    

    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 4
    }
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if(cell.respondsToSelector(Selector("setSeparatorInset:"))) {
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        if(cell.respondsToSelector(Selector("setLayoutMargins:"))) {
            cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
        }
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        return 50
    }
    
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        
      
        let v:UIView = UIView(frame: CGRectMake(0, 0, 0, 0))
        v.backgroundColor = UIColor.colorWithRGB(207, g: 209, b: 209)
        return v
        
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        

        
      let cell = tableView.dequeueReusableCellWithIdentifier("mySelfCell", forIndexPath: indexPath) as! mySelfCell
        
     let cellNib = UINib(nibName: "mySelfCell", bundle: nil)
        self.tableView.registerNib(cellNib, forCellReuseIdentifier: "mySelfCell")
        
        if indexPath.row == 0 {
            cell.nameLbl.text = "用户信息"
            cell.iv.hidden = false
            cell.Switch.hidden = true
        }
        else if indexPath.row == 1{
            cell.nameLbl.text = "商城"
            cell.iv.hidden = false
            cell.Switch.hidden = true
        }
        else if indexPath.row == 2{
            cell.nameLbl.text = "投稿、提问、反馈"
            cell.iv.hidden = false
            cell.Switch.hidden = true

        }
        else if indexPath.row == 3{
            cell.nameLbl.text = "设置"
            cell.iv.hidden = false
            cell.Switch.hidden = true
        }
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
        if indexPath.row == 0{//用户信息
              //分为三种个人,美容院,企业
            if !ApplicationDelegate.isLogin(){
                let sb = UIStoryboard(name: "Login", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("LoginViewController")
                self.navigationController?.pushViewController(vc, animated: true)
                
            }else{
               if !publicMethod.isComplete(){
                let sb = UIStoryboard.init(name: "Login", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("ChooseTypeTableViewController") as! ChooseTypeTableViewController
            
                self.navigationController?.pushViewController(vc, animated: true)
               }else{
                let type = (NSUserDefaults.standardUserDefaults().objectForKey("k_UD_type")) as! String
                
                if type == "个人"{
                    let vc = (self.storyboard?.instantiateViewControllerWithIdentifier("UserInfoTC"))
                    self.hidesBottomBarWhenPushed = true
                    self.navigationController?.pushViewController(vc!, animated: true)
                    self.hidesBottomBarWhenPushed = false
                }
                if type == "企业"{
                    let vc = (self.storyboard?.instantiateViewControllerWithIdentifier("UserCompnayVC")) as! UserCompnayVC
                    vc.type = "企业"
                    self.hidesBottomBarWhenPushed = true
                    self.navigationController?.pushViewController(vc, animated: true)
                    self.hidesBottomBarWhenPushed = false
                }
                if type == "美容院"{
                    let vc = (self.storyboard?.instantiateViewControllerWithIdentifier("UserBeautyVC")) as! UserBeautyVC
                    vc.type = "美容院"
                    self.hidesBottomBarWhenPushed = true
                    self.navigationController?.pushViewController(vc, animated: true)
                    self.hidesBottomBarWhenPushed = false
                }
                
               }
            }
       
        }
        else if indexPath.row == 1{//商城
            if !ApplicationDelegate.isLogin(){
                let sb = UIStoryboard(name: "Login", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("LoginViewController")
                self.navigationController?.pushViewController(vc, animated: true)
                
            }else{
                let vc = (self.storyboard?.instantiateViewControllerWithIdentifier("IntegralMallVC"))
                self.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(vc!, animated: true)
                self.hidesBottomBarWhenPushed = false
            }
        }
        else if indexPath.row == 2{//投稿
            print("投稿")
            if !ApplicationDelegate.isLogin(){
                let sb = UIStoryboard(name: "Login", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("LoginViewController")
                self.navigationController?.pushViewController(vc, animated: true)
                
            }else{
                let vc = self.storyboard?.instantiateViewControllerWithIdentifier("FeedbackVC") as! FeedbackVC
                self.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(vc, animated: true)
                self.hidesBottomBarWhenPushed = false
            }
        }
        else if indexPath.row == 3{//设置
            let vc = (self.storyboard?.instantiateViewControllerWithIdentifier("SetupTableViewController"))
            self.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(vc!, animated: true)
            self.hidesBottomBarWhenPushed = false
        }
    }

    @IBAction func didSelectedListBnt(sender: UIButton)
    {
        let tag = sender.tag
        switch tag {
        case 0:
            if !ApplicationDelegate.isLogin(){
                let sb = UIStoryboard(name: "Login", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("LoginViewController")
                self.navigationController?.pushViewController(vc, animated: true)
                
            }else{
                let vc = (self.storyboard?.instantiateViewControllerWithIdentifier("IntegralAccountsVC")) as! IntegralAccountsVC
                vc.type = "1"
                self.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(vc, animated: true)
                self.hidesBottomBarWhenPushed = false
            }
            
        case 1:
            if !ApplicationDelegate.isLogin(){
                let sb = UIStoryboard(name: "Login", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("LoginViewController")
                self.navigationController?.pushViewController(vc, animated: true)
                
            }else{
                let vc = (self.storyboard?.instantiateViewControllerWithIdentifier("IntegralAccountsVC")) as! IntegralAccountsVC
                vc.type = "2"
                self.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(vc, animated: true)
                self.hidesBottomBarWhenPushed = false
            }
            
        case 2:
            if !ApplicationDelegate.isLogin(){
                let sb = UIStoryboard(name: "Login", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("LoginViewController")
                self.navigationController?.pushViewController(vc, animated: true)
                
            }else{
                let vc = (self.storyboard?.instantiateViewControllerWithIdentifier("collectionVC"))
                self.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(vc!, animated: true)
                self.hidesBottomBarWhenPushed = false
            }
           
        case 3:
            if !ApplicationDelegate.isLogin(){
                let sb = UIStoryboard(name: "Login", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("LoginViewController")
                self.navigationController?.pushViewController(vc, animated: true)
                
            }else{
                let vc = (self.storyboard?.instantiateViewControllerWithIdentifier("myConcernVC"))
                self.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(vc!, animated: true)
                self.hidesBottomBarWhenPushed = false
            }
            
        case 4:
            if !ApplicationDelegate.isLogin(){
                let sb = UIStoryboard(name: "Login", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("LoginViewController")
                self.navigationController?.pushViewController(vc, animated: true)
                
            }else{
                let vc = (self.storyboard?.instantiateViewControllerWithIdentifier("myFansVC"))
                self.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(vc!, animated: true)
                self.hidesBottomBarWhenPushed = false
            }
           
        case 5:
            if !ApplicationDelegate.isLogin(){
                let sb = UIStoryboard(name: "Login", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("LoginViewController")
                self.navigationController?.pushViewController(vc, animated: true)
                
            }else{
                let vc = (self.storyboard?.instantiateViewControllerWithIdentifier("myTopicVC"))
                self.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(vc!, animated: true)
                self.hidesBottomBarWhenPushed = false
            }
            
        case 6:
            if !ApplicationDelegate.isLogin(){
                let sb = UIStoryboard(name: "Login", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("LoginViewController")
                self.navigationController?.pushViewController(vc, animated: true)
                
            }else{
                let vc = (self.storyboard?.instantiateViewControllerWithIdentifier("myActivityVC"))
                self.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(vc!, animated: true)
                self.hidesBottomBarWhenPushed = false
            }
            
        case 7:
            if !ApplicationDelegate.isLogin(){
                let sb = UIStoryboard(name: "Login", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("LoginViewController")
                self.navigationController?.pushViewController(vc, animated: true)
                
            }else{
                let vc = (self.storyboard?.instantiateViewControllerWithIdentifier("advertisesVC")) as! advertisesVC
                vc.type = self.joinBtn.titleLabel?.text
                self.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(vc, animated: true)
                self.hidesBottomBarWhenPushed = false
            }
            
        case 8:
            if !ApplicationDelegate.isLogin(){
                let sb = UIStoryboard(name: "Login", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("LoginViewController")
                self.navigationController?.pushViewController(vc, animated: true)
                
            }else{
                let vc = (self.storyboard?.instantiateViewControllerWithIdentifier("transferVC"))
                self.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(vc!, animated: true)
                self.hidesBottomBarWhenPushed = false
            }
           
        default : print(0)
            
        }
    
    }
    @IBAction func ivBntClicked(sender: UIButton){
         if NSUserDefaults.standardUserDefaults().boolForKey("k_UD_isComplete"){
            if !ApplicationDelegate.isLogin(){
                let vc = UIStoryboard(name: "Login", bundle: nil).instantiateViewControllerWithIdentifier("LoginViewController")
                self.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(vc, animated: true)
                self.hidesBottomBarWhenPushed = false

            }else{
                let sb = UIStoryboard.init(name: "NewsList", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("UserHomeTableViewController") as! UserHomeTableViewController
                
                vc.userId = Int(self.modelList!.id)
                
                self.navigationController?.pushViewController(vc, animated: true)

            }
            
         }else{
            if ApplicationDelegate.isLogin(){
                let sb = UIStoryboard.init(name: "Login", bundle: nil)
                let vc = sb.instantiateViewControllerWithIdentifier("ChooseTypeTableViewController") as! ChooseTypeTableViewController
                
                self.navigationController?.pushViewController(vc, animated: true)
            }else{
                let vc = UIStoryboard(name: "Login", bundle: nil).instantiateViewControllerWithIdentifier("LoginViewController")
                self.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(vc, animated: true)
                self.hidesBottomBarWhenPushed = false
            }
            
        }
    }

}
